import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Files;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.utils.IOUtils;

public class TestZip {

	public static void main(String[] args) throws Exception {
		ArchiveOutputStream gzippedOut = new ArchiveStreamFactory()
			    .createArchiveOutputStream("zip", new FileOutputStream("D:\\zip.zip"));
		
		File sourceFile = new File("D:\\gen_code\\");
		File[] listFiles = sourceFile.listFiles();
//		for (File f : listFiles) {
//	        ArchiveEntry entry = gzippedOut.createArchiveEntry(f, f.getName());
//	        gzippedOut.putArchiveEntry(entry);
//	        if (f.isFile()) {
//	            try (InputStream i = Files.newInputStream(f.toPath())) {
//	                IOUtils.copy(i, gzippedOut);
//	            }
//	        }else {
//	        	File[] subFileList = f.listFiles();
//	        	for(File sf : subFileList) {
//	        		
//	        	}
//	        }
//	        gzippedOut.closeArchiveEntry();
//	    }
		for (File f : listFiles) {
			zipSubFiles(f,gzippedOut,"");
		}
		gzippedOut.close();
	}
	
	private static void zipSubFiles(File sourceFile,ArchiveOutputStream gzippedOut,String path) throws Exception {
		File[] listFiles = sourceFile.listFiles();
		for (File f : listFiles) {
			String entryPath = path + f.getName();
	        ArchiveEntry entry = gzippedOut.createArchiveEntry(f, entryPath);
	        gzippedOut.putArchiveEntry(entry);
	        if (f.isFile()) {
	            try (InputStream i = Files.newInputStream(f.toPath())) {
	                IOUtils.copy(i, gzippedOut);
	            }
	            gzippedOut.closeArchiveEntry();
	        }else {
	        	gzippedOut.closeArchiveEntry();
	        	zipSubFiles(f,gzippedOut,entryPath + "/");
	        }
	    }
	}
}
